package df.servlet.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import df.bean.db.conn.DBConnection;
import df.jsp.Util;

/**
 * Servlet implementation class RetrieveMutiData
 */
public class RetrieveMultiData extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */

    DBConnection con;
    
    public void init() throws ServletException {
        super.init();
        con = new DBConnection();
        con.connectToServer();
    }
    
    @Override
    public void destroy(){
        super.destroy();
        con.freeConnection();
    }
   

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
	   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
			    throws ServletException, IOException {
		   response.setContentType("text/xml; charset=UTF-8");

	        PrintWriter out = response.getWriter();
	        out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");


	        //
	        // Process request
	        //
	        
	        request.setCharacterEncoding("UTF-8");
	        if (request.getParameter("TABLE") == null) {
	            out.print("<RECORDS></RECORDS>");
	            return;
	        }

	        String str = "<RECORDS>";
	        String table = request.getParameter("TABLE");
	        String cond = request.getParameter("COND");
	        
	        try {
	            String query = String.format("SELECT * FROM %1$s", table);
	            if (cond != null) {
	                query += " WHERE " + cond;
	            }
	            //System.out.println(query);
	            String[] listFields = con.getColumnNames(table);
	            
	            ResultSet rs = null;
	            try{
		            rs = con.executeQuery(query);
		            while (rs != null && rs.next()) {
		                str += "<" + table + ">";
		                //for (int i = 0; i < listFields.length; i++) {
		                    str += String.format("<%1$s>%2$s</%1$s>", listFields[5], Util.formatHTMLString(rs.getString(listFields[5]), false));
		                //}
		                
		                str += "</" + table + ">";
		        
		            }
	            }catch(Exception e){
	            	System.out.println("Retrieve Data Error : "+e+"\n");
	            }

	            if (rs != null) {
	                rs.close();
	            }
	            //con.freeConnection();
	        }
	        catch (Exception e) {
	            System.out.println("Retrieve Data Error : "+e+"\n");
	        }
	        str += "</RECORDS>";
	        out.print(str);
	   }

}